import types from './types.js'
import newRoutes from '@/router/routers'
import {routes} from '@/router/routes.js'
import {handleRoutesMap,routesMap} from '@/router/routers/router_map.js'
// import {routesMap} from '@/router/routes.js'
// import routesMap from '../router/routers/router_map'
/* 最早期的方法，已弃用
const hasAccess = (route,rulesIds) => {
	return rules.some(el => {
		if (route.name == el.name && rulesIds.some(item=> el.id==item)){
			return true;
		}
		return false;
	});
};
const getAccessRoutes = (routesMap,rulesIds) => {
	let newRouter = routesMap.filter(item => {
		if (item.children && Array.isArray(item.children)) {
			 item.children = item.children.map(item => {
				(!item.meta) && (item.meta = {isShowMenu:false});
				if (hasAccess({...item},rulesIds)) {
					item.meta.isShowMenu = true;
				}else {
					item.meta.isShowMenu = false;
				}
				 return item;
			 });
			(item.children.every(item => item.meta.isShowMenu === false)) && (!(item.meta) && (item.meta = {isShowMenu:false}));
			return item.children.length;
		}
	});
	newRouter[newRouter.length] = routesMap[routesMap.length-1];
	return newRouter;
};*/
/* 此方法，后台添加菜单，前端路由组件必须添加，才能显示该菜单
const hasAccess = (routerList) => {
	let newRoutes = [];
	for (let key in routerList){
		let routerListItem = routerList[key];
		for (let k in newRoutesMap){
			// 前端与后台需要约定路由的 name 值
			let routesMapItem = newRoutesMap[k];
			if (routerListItem.name == routesMapItem.name){
				if (routesMapItem.children && routesMapItem.children.length > 0){
					// 二级菜单
					routesMapItem.path = routerListItem.path;
					routesMapItem.meta.title = routerListItem.title;
					routesMapItem.meta.icon = routerListItem.icon;
					routesMapItem.children = getAccessChildrenRoutes(routerListItem,routesMapItem.children);
					routesMapItem.children && routesMapItem.children.map(item => newRoutes.push(item));
					//newRoutes = newRoutes.concat(hasAccess(routerListItem.children));
				}else{
					newRoutes.push(routesMapItem);
				}
				// menus.push({
				// 	path:routerListItem.path,
				// 	name:routerListItem.name,
				// 	meta:routesMapItem.meta,
				// 	children:routesMapItem.children
				// });
				menus.push(routesMapItem);
				newRoutesMap.splice(k,1);
				break;
			}
		}
		menus.push();
	}
	return newRoutes;
};*/
let newRoutesMap = routesMap.slice(0,routesMap.length-1);
// 处理请求回来的权限菜单路由数据组装
let menus = [];// 存放左侧菜单
// 此方法，后台添加菜单，前端路由组件不需要添加，也能显示该菜单，访问只不过是 404 ，因为前端没对应组件
const hasAccess = (routerList) => {
	let newRoutes = [];
	for (let key in routerList){
		let routerListItem = routerList[key];
		let r = {path:routerListItem.path, name:routerListItem.name, meta:{title:routerListItem.title,icon:routerListItem.icon}};
		let flag = true;
		for (let k in newRoutesMap){
			// 前端与后台需要约定路由的 name 值
			let routesMapItem = newRoutesMap[k];
			if (routerListItem.name == routesMapItem.name){
				r.path = routerListItem.path;
				r.name = routerListItem.name;
				// r.meta.title = routesMapItem.meta.title;
				// r.meta.icon = routesMapItem.meta.icon;
				//r.meta = routesMapItem.meta;
				if (routesMapItem.children && routesMapItem.children.length > 0){
					r.children = getAccessChildrenRoutes(routerListItem,routesMapItem.children);
					routesMapItem.children.map(item => newRoutes.push(item));
				}else{
					newRoutes.push(routesMapItem);
				}
				newRoutesMap.splice(k,1);
				flag = false;
				break;
			}
		}
		if(flag && routerListItem.children && routerListItem.children.length > 0){
			r.children = handleMenu(routerListItem.children);
		}
		menus.push(r);
	}
	return newRoutes;
};
// 递归覆盖子级路由属性
const getAccessChildrenRoutes = (routerListChildren,routesMapItemChildren) => {
	let chidlren = [];
	routerListChildren.children && routerListChildren.children.length > 0 && routerListChildren.children.map(item => {
		routesMapItemChildren.length > 0 && routesMapItemChildren.map(router => {
			if (router.name == item.name){
				router.path = item.path;
				router.meta.title = item.title;
				router.meta.icon = item.icon;
				let child = getAccessChildrenRoutes(item.children,routesMapItemChildren);
				if (child && child.length > 0){
					item.children = child;
				}
				chidlren.push(router);
			}
		});
		return item;
	});
	return chidlren;
};
// 处理请求回来的菜单的数据结构
const handleMenu = (routerList) => {
	let newMenu = [];
	for (let key in routerList){
		let routerListItem = routerList[key];
		let r = {path:routerListItem.path, name:routerListItem.name, meta:{title:routerListItem.title,icon:routerListItem.icon}};
		if(routerListItem.children && routerListItem.children.length > 0){
			r.children = handleMenu(routerListItem.children);
		}
		newMenu.push(r);
	}
	return newMenu;
};

const actions = {
    setToggleDevice({commit},device){
        commit(types.TOGGLE_DEVICE,device);
    },
	setAuthToken ({commit},isAuthToken) {
		commit(types.SET_TOKEN,isAuthToken);
	},
	clearLoginOut ({commit}) {
		commit(types.SET_USER,null);
		commit(types.SET_SIDERLIST,null);
		commit(types.SET_NEWROUTES,newRoutes);
		commit(types.SET_TOKEN,false);
	},
	setSiderList ({commit},siderList) {
		commit(types.SET_SIDERLIST,siderList);
	},
	/*getUserInfo ({commit},userInfo) {
		return new Promise((resolve,reject) => {
			try {
				const routers = userInfo.menus;
				if (roles && (roles == 1 || roles.length > 0)) {
					userInfo.roles = [roles];
					commit(types.SET_USER,userInfo);
				} else {
					reject('getInfo: roles must be a non-null array !')
				}
				resolve(userInfo);
			} catch(e) {
				console.log('getUserInfo',error);
				reject(error);
			}
		});
	},*/
	concatRlues ({commit},userInfo) {
		return new Promise((resolve,reject) => {
			try {
				let routerList = [];
				if (userInfo.menus && Array.isArray(userInfo.menus) && userInfo.menus.length > 0){
					routerList = userInfo.menus;
				}
				// true开启权限验证模式 ，false 不使用权限验证
				//routerList = userInfo.isAuth==false?handleRoutesMap:hasAccess(routerList);
				/*let key = newRoutes.findIndex(item => item.path=="/");
				if (userInfo.isAuth==false){
					//routerList = handleRoutesMap;
					newRoutes[key].children = [...newRoutes[key].children,handleRoutesMap.pop()];
				}else{
					//routerList = hasAccess(routerList);
					newRoutes[key].children = [...newRoutes[key].children,...hasAccess(routerList),routesMap.pop()];
				}*/
				let key = newRoutes.findIndex(item => item.path=="/");
				let child = newRoutes[key].children;
                //newRoutes[key].children = userInfo.isAuth==false ? [...child,handleRoutesMap.pop()] : [...child,...hasAccess(routerList),routesMap.pop()];
				newRoutes[key].children = userInfo.isAuth==true ? [...child,...hasAccess(routerList),routesMap.pop()] : [...child,handleRoutesMap.pop()];
				commit(types.SET_NEWROUTES,newRoutes);
				commit(types.SET_SIDERLIST,routes.filter(item => item.isMenu==true).concat(menus));
				commit(types.SET_USER,userInfo);
				resolve(newRoutes);
			 } catch(error) {
				console.log('%c ! filter router error','background:#000;color:#bada55',error);
			 	reject(error);
			 }
		});
	}
};

export default actions;